from typing import Optional

from ninja import FilterSchema, Field, Schema
from django.db.models import Q


class OrderInfoFiltersSchema(FilterSchema):
    """用户订单筛选"""
    order_id: Optional[str] = Field(default="", description="订单编号", q="order_id__contains")
    name: Optional[str] = Field(default="", description="收货人", q="address__consignee__contains")
    transaction_status: Optional[int] = Field(default=0, description="交易状态")
    delivery_status: Optional[int] = Field(default=0, description="配送状态")


class ManagerOrderFiltersSchema(FilterSchema):
    """用户订单筛选"""
    order_id: Optional[str] = Field(default="", description="订单编号", q="order_id__contains")
    buyer: Optional[str] = Field(default=0, description="买家",
                                 q=["user__nickname__contains", "user__openid__contains"])
    consignee: Optional[str] = Field(default=0, description="收货人", q="address__consignee__contains")
    transaction_status: Optional[int] = Field(default=0, description="交易状态")
    delivery_status: Optional[int] = Field(default=0, description="配送状态")
    create_time: Optional[dict] = None

    def filter_create_time(self, value: dict):
        if not value:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(create_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(create_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(create_time__gt=value.get('start'), create_time__lt=value.get('end'))
        return Q()


class ManagerVipOrderFiltersSchema(FilterSchema):
    """VIP订单筛选"""
    order_id: Optional[str] = Field(default="", description="订单编号", q="order_id__contains")
    buyer: Optional[str] = Field(default="", description="买家",
                                 q=["user__nickname__contains", "user__openid__contains",
                                    "user__phone_number__contains"])
    transaction_time: Optional[dict] = None
    create_time: Optional[dict] = None

    def filter_create_time(self, value: dict):
        if not value:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(create_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(create_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(create_time__gt=value.get('start'), create_time__lt=value.get('end'))
        return Q()

    def filter_transaction_time(self, value: dict):
        if not value:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(transaction_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(transaction_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(transaction_time__gt=value.get('start'), transaction_time__lt=value.get('end'))
        return Q()


class ManagerAgentOrderFiltersSchema(FilterSchema):
    """代理订单筛选"""
    order_id: Optional[str] = Field(default="", description="订单编号", q="order_id__contains")
    operator: Optional[str] = Field(default="", description="操作者",
                                    q=["operator__nickname__contains", "operator__openid__contains",
                                       "operator__phone_number__contains"])
    exec_user: Optional[str] = Field(default="", description="被操作者",
                                     q=["exec_user__nickname__contains", "exec_user__openid__contains",
                                        "exec_user__phone_number__contains"])
    op_type: Optional[int] = Field(default=0, description="操作类型")
    transaction_time: Optional[dict] = None
    create_time: Optional[dict] = None

    def filter_create_time(self, value: dict):
        if not value:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(create_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(create_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(create_time__gt=value.get('start'), create_time__lt=value.get('end'))
        return Q()

    def filter_transaction_time(self, value: dict):
        if not value:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(transaction_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(transaction_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(transaction_time__gt=value.get('start'), transaction_time__lt=value.get('end'))
        return Q()


class OrderUpdateLogisticsSchema(Schema):
    """更新物流信息"""
    logistics_id: str = Field(default="", description="物流单号")
    Logistics_company: str = Field(default="", description="物流公司")


class OrderRefundSchema(Schema):
    """订单退款"""
    after_sales_status: int = Field(default=0, description="售后状态")
    refund_amount: str = Field(default="", description="退款金额")
